﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using HRMS_Model;
using DataManager;
using System.Diagnostics;
using System.Web.Mvc;
using HRMS.Models.ModelBinders;

namespace HRMS.Models
{
    public class EmployeeModels
    {
        public static bool AddEmployee(Employee employee)
        {
            try
            {
                using (var employeeEntity = new HRMSEntities())
                {
                    employeeEntity.Employees.AddObject(employee);
                    employeeEntity.SaveChanges();
                    return true;
                }
            }
            catch (Exception ex)
            {
                Debug.Write(ex.Message);
                return false;
            }
        }

        public static List<Employee> GetEmployees(string sortExpression, string sortDirection, int pageIndex, int pageSize, int pageCount)
        {
            using (var employeeEntity = new HRMSEntities())
            {
                return employeeEntity.Employees.Include("Department").Include("Designation").OrderBy("it." + sortExpression + " " + sortDirection).Skip(pageIndex * pageSize).Take(pageCount * pageSize).ToList();
            }
        }

        public static int GetEmployeesCount()
        {
            using (var employeeEntity = new HRMSEntities())
            {
                return employeeEntity.Employees.Count();
            }
        }

        public static bool UpdateEmployee([ModelBinder(typeof(CustomModelBinder))]Employee employee)
        {
            try
            {
                using (var employeeEntity = new HRMSEntities())
                {
                    var empToEdit = employeeEntity.Employees.FirstOrDefault(r => r.EmpId == employee.EmpId);
                    if (empToEdit != null)
                    {
                        empToEdit.FirstName = employee.FirstName;
                        empToEdit.MiddleName = employee.MiddleName;
                        empToEdit.LastName = employee.LastName;
                        empToEdit.Address = employee.Address;
                        empToEdit.City = employee.City;
                        empToEdit.State = employee.State;
                        empToEdit.Zip = employee.Zip;
                        empToEdit.MobileNo = employee.MobileNo;
                        empToEdit.Extension = employee.Extension;
                        empToEdit.HomePhone = employee.HomePhone;
                        empToEdit.Email = employee.Email;
                        empToEdit.Gender = employee.Gender;
                        empToEdit.DateOfBirth = employee.DateOfBirth;
                        empToEdit.MartialStatus = employee.MartialStatus;
                        empToEdit.DegreeAttained = employee.DegreeAttained;
                        empToEdit.Skills = employee.Skills;
                        empToEdit.Experience = employee.Experience;
                        empToEdit.ResumeSize = employee.ResumeSize;
                        empToEdit.Resume = employee.Resume;
                        empToEdit.DesignationId = employee.DesignationId;
                        empToEdit.DepartmentId = employee.DepartmentId;
                    }
                    employeeEntity.SaveChanges();
                    return true;
                }
            }
            catch (Exception ex)
            {
                Debug.Write(ex.Message);
                return false;
            }
        }

        public static bool DeleteEmployee(int id)
        {
            try
            {
                using (var employeeEntity = new HRMSEntities())
                {
                    employeeEntity.Employees.DeleteObject(employeeEntity.Employees.First(e => e.EmpId == id));
                    employeeEntity.SaveChanges();
                    return true;
                }
            }
            catch (Exception ex)
            {
                Debug.Write(ex.Message);
                return false;
            }
        }

        public static List<string> DesignationsList()
        {
            var designationList = new List<string>();
            try
            {
                using (var designationEntity = new HRMSEntities())
                {
                    var designations = designationEntity.Designations.ToList();
                    designationList.AddRange(designations.Select(designation => designation.DesignationName));
                }
                return designationList;
            }
            catch (Exception ex)
            {
                Debug.Write(ex.Message);
                return designationList;
            }
        }
    }
}